import Gobj from "./Gobj";
import GUniverse from "./GUniverse";
import GEnemy from "./GEnemy";
import VEnemy from "./VEnemy";
import GMap from "./GMap";
import GTower from "./GTower";
import GBullet from "./GBullet";
import Gstatic from "./Gstatic";
import GPlayer from "./GPlayer";

export default class GManager extends Gobj {

    constructor(uid: number) {
        super();
        this.count = 0;
        this.uid = uid;
        this.id = this.count++;//0

        this.player1 = new GPlayer();
        this.player1.hp = 3;
        this.player1.uid = uid;
        this.player1.id = this.count++;//1
        this.player1.pid = this.player1.id;

        this.player2 = new GPlayer();
        this.player2.hp = 3;
        this.player2.uid = uid;
        this.player2.id = this.count++;//2
        this.player2.pid = this.player2.id;
    }

    get gameover() {
        return this.v0 == 1;
    }

    set gameover(b: boolean) {
        this.v0 = b ? 1 : 0;
    }

    get cd() {
        return this.v1;
    }

    set cd(ii: number) {
        this.v1 = ii;
    }

    private player1: GPlayer;
    private player2: GPlayer;

    public getPlayer(pid: number): GPlayer {
        return pid == 1 ? this.player1 : this.player2;
    }

    syncTime(other: Gobj) {
        //console.log(" manager 开始回滚  ");        
        super.syncTime(other);
        var om = other as GManager;
        // this.monsterMap.syncTime(this.uid, om.monsterMap);
        // this.bulletMap.syncTime(this.uid,om.bulletMap);
        // this.towerMap.syncTime(this.uid, om.towerMap);
        this.player1.syncTime(om.player1);
        this.player2.syncTime(om.player2);

    }


    timeline(sf: number, lf: number, fms: number, keyf: boolean, u: GUniverse, ac: any) {
        this.ct = fms;
        //var monster_len = this.monsterMap.objArr.length;
        if (this.cd < this.ct) {//&& monster_len <= 40) { //生成 怪物           
            this.cd = this.ct + 1500;
            let r = u.random.seededRandom();
            let hp = (300 + Math.floor(100 * r)) * 100;

            var obj = new GEnemy();
            obj.type = Gobj.GEnemy;
            obj.id = this.count++;
            obj.uid = this.uid;
            obj.st = fms;
            obj.hp = hp;
            obj.pid = this.player1.pid;
            this.player1.monsterMap.addObj(obj);

            obj = new GEnemy();
            obj.type = Gobj.GEnemy;
            obj.id = this.count++;
            obj.uid = this.uid;
            obj.st = fms;
            obj.hp = hp;
            obj.pid = this.player2.pid;
            this.player2.monsterMap.addObj(obj);
        }

        if (ac) {
            for (let j = 0; j < ac.length; j++) {
                this.action(ac[j], sf, lf, fms, keyf, u);
            }
        }

        this.player1.timeline(sf, lf, fms, keyf, u, ac);
        this.player2.timeline(sf, lf, fms, keyf, u, ac);

        if (!this.gameover && this.ct >= 1000 * 70) {
            this.gameover = true;
            console.log("sf ", sf, " lf ", lf, " fms ", fms, " 游戏时间到了 结束！ ", this.ct);
            console.log("最后的随机种子 ", u.random.seed);
            console.log(" uid ", this.uid);
            //console.log(this);            
        }
    }

    action(ass: string, sf: number, lf: number, fms: number, keyf: boolean, u: GUniverse) {
        var player: GPlayer
        var id = this.count++;

        var ack = ass.split(",");
        if (ack[0] == "0") {//0 建造炮塔
            if (ack[1] == "1") {
                player = this.player1;
                this.count++;
            } else {
                player = this.player2;
                id = this.count++;
            }

            if (player.towerMap.objArr.length < 15) {

                var tower: GTower = new GTower();
                var mi = player.towerMap.objArr.length;
                var mx = Math.floor(mi % 5);
                var my = Math.floor(mi / 5);

                tower.x = 80 * 100 + 8500 * mx;
                tower.y = 80 * 100 + 8500 * my;

                tower.id = id;
                tower.st = fms;
                tower.uid = this.uid;
                tower.pid = player.pid;
                player.towerMap.addObj(tower);
            }
        }
        else if (ack[0] == "9") {//9 游戏结算

        }
    }


}